package com.macro.mall.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.convert.PmsProductCategoryAttrCategoryConvert;
import com.macro.mall.model.pms.PmsProductAttributeSetDO;
import com.macro.mall.model.pms.PmsProductCategoryAttributeSetMappingDO;
import com.macro.mall.service.PmsProductAttributeSetService;
import com.macro.mall.service.PmsProductCategoryAttributeSetMappingDomainService;
import com.macro.mall.vo.pms.PmsProductAttributeCategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品属性分类管理Controller
 * Created by macro on 2018/4/26.
 */
@Controller
@Api(tags = "PmsProductCategoryAttributeSetMappingController")
@Tag(name = "PmsProductCategoryAttributeSetMappingController", description = "商品属性集合管理")
@RequestMapping("/productCategoryAttributeSetMapping")
public class PmsProductCategoryAttributeSetMappingController {
    @Autowired
    private PmsProductAttributeSetService productAttributeCategoryService;

    @Autowired
    private PmsProductCategoryAttributeSetMappingDomainService productCategoryAttrCategoryMappingDomainService;

    @Autowired
    private PmsProductCategoryAttrCategoryConvert pmsProductCategoryAttrCategoryConvert;


    @ApiOperation("根据分类ID获取所有商品属性分类列表")
    @RequestMapping(value = "/listByCategoryId", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsProductAttributeCategoryVO>> listByCategoryId(@RequestParam() Long categoryId) {
            //查询映射
            Wrapper<PmsProductCategoryAttributeSetMappingDO> queryWrapper = Wrappers.lambdaQuery(PmsProductCategoryAttributeSetMappingDO.class)
                    .select(PmsProductCategoryAttributeSetMappingDO::getAttrSetId)
                    .eq(PmsProductCategoryAttributeSetMappingDO::getCategoryId,categoryId);
            List<PmsProductCategoryAttributeSetMappingDO> list =productCategoryAttrCategoryMappingDomainService.list(queryWrapper);
            if(CollectionUtil.isEmpty(list)){
                return CommonResult.success(new ArrayList<>());
            }
            //查询属性分类列表
            Wrapper<PmsProductAttributeSetDO> attrQueryWrapper = Wrappers.lambdaQuery(PmsProductAttributeSetDO.class)
                    .in(PmsProductAttributeSetDO::getId,list.stream().map(PmsProductCategoryAttributeSetMappingDO::getAttrSetId).collect(Collectors.toList()));
          List<PmsProductAttributeSetDO>  productAttributeCategoryList = productAttributeCategoryService.list(attrQueryWrapper);

        return CommonResult.success(pmsProductCategoryAttrCategoryConvert.toVoList(productAttributeCategoryList));
    }

    @ApiOperation("删除商品分类挂接的属性分类")
    @RequestMapping(value = "/deleteMapping", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Boolean> deleteMapping(@RequestParam("categoryId") Long categoryId,
                               @RequestParam("attrCategoryId") Long attrCategoryId) {
        Wrapper<PmsProductCategoryAttributeSetMappingDO> queryWrapper = Wrappers.lambdaQuery(PmsProductCategoryAttributeSetMappingDO.class)
                .eq(PmsProductCategoryAttributeSetMappingDO::getCategoryId,categoryId)
                .eq(PmsProductCategoryAttributeSetMappingDO::getAttrSetId,attrCategoryId);
        boolean done = productCategoryAttrCategoryMappingDomainService.remove(queryWrapper);
        if (done) {
            return CommonResult.success(done);
        } else {
            return CommonResult.failed();
        }
    }



    @ApiOperation("删除商品分类挂接的属性分类")
    @RequestMapping(value = "/addMapping", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Boolean> addMapping(@RequestParam("categoryId") Long categoryId,
                                               @RequestParam("attrCategoryId") Long attrCategoryId) {

        PmsProductCategoryAttributeSetMappingDO mappingDO = new PmsProductCategoryAttributeSetMappingDO();
        mappingDO.setCategoryId(categoryId);
        mappingDO.setAttrSetId(attrCategoryId);
        boolean done = productCategoryAttrCategoryMappingDomainService.save(mappingDO);
        if (done) {
            return CommonResult.success(done);
        } else {
            return CommonResult.failed();
        }
    }

}
